package com.ellricky.baselib.adapter;

import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.andr.common.ui.adapter.BaseRecyclerAdapter;
import com.andr.common.ui.adapter.SmartViewHolder;
import com.ellricky.baselib.R;
import com.ellricky.baselib.entity.PersonEntity;
import com.ellricky.baselib.entity.PersonItemEntity;
import com.ellricky.baselib.listener.PersonListListener;

import java.util.List;

public class PersonAdapter extends BaseRecyclerAdapter<PersonEntity> {

    private Activity activity;
    private Fragment fragment;
    private PersonListListener listListener;

    private boolean choosePage = false;
    private boolean showRateBar = false;
    private boolean showClassNum = false;
    private boolean showSex = false;

    public void setShowSex(boolean showSex) {
        this.showSex = showSex;
    }

    public void setShowRateBar(boolean showRateBar) {
        this.showRateBar = showRateBar;
    }

    public void setChoosePage(boolean choosePage) {
        this.choosePage = choosePage;
    }

    public void setShowClassNum(boolean showClassNum) {
        this.showClassNum = showClassNum;
    }

    public PersonAdapter(Activity activity, Fragment fragment) {
        this(activity, fragment, null);
    }

    public PersonAdapter(Activity activity, Fragment fragment, PersonListListener listListener) {
        super(R.layout.base_layout_adapter_person);

        if (fragment != null) {
            this.fragment = fragment;
            this.activity = fragment.getActivity();
        }

        if (activity != null) {
            this.activity = activity;
        }

        this.listListener = listListener;

    }


    @Override
    public void onBindViewHolder(SmartViewHolder holder, PersonEntity model, final int position) {
        holder.text(R.id.tvCapital, model.getShortName());

        // refresh
        final List<PersonItemEntity> itemEntityList = model.getData();
        if (itemEntityList != null && itemEntityList.size() > 0) {
            RecyclerView rvH = holder.itemView.findViewById(R.id.rvH);
            rvH.setLayoutManager(createLM(activity));
            PersonItemAdapter adapter = createItemAdapter(activity, fragment);
            adapter.setPersonListListener(listListener);
            adapter.setShowRateBar(showRateBar);
            adapter.setChoosePage(choosePage);
            adapter.setShowClassNum(showClassNum);
            adapter.setShowSex(showSex);
            adapter.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    if (listListener != null) {
                        if (!choosePage){
                            listListener.onClickPersonItem(itemEntityList.get(i));
                        }

                    }
                }
            });
            rvH.setAdapter(adapter);
            adapter.refresh(itemEntityList);
        }


    }

    private LinearLayoutManager createLM(Activity activity) {
        GridLayoutManager llm = new GridLayoutManager(activity,3);
        return llm;
    }

    private PersonItemAdapter createItemAdapter(Activity activity, Fragment fragment) {
        return new PersonItemAdapter(activity, fragment);
    }

}
